package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.mapper.havexml.XtjcKsywxtMapper;
import com.sinog.biz.mapper.havexml.other.SwitchToOriginalInvestigationOrganMapper;
import com.sinog.biz.service.business.WorkCoordinationService;
import com.sinog.biz.util.CodeUrlUtils;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 业务协调 - 业务协调 - 跨省业务协调  WorkCoordinationServiceImpl
 * @author -
 * @date 2020年2月5日 20点48分
 */
@Service
public class WorkCoordinationServiceImpl implements WorkCoordinationService {

    @Resource
    private XtjcKsywxtMapper workCoordinationMapper;
    @Resource
    private SwitchToOriginalInvestigationOrganMapper switchToOriginalInvestigationOrganMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 跨省业务协调列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param departId 当前登录用户部门编号
     * @param level 当前登录用户level
     * @param orgid 当前登录用户上级编号
     * @param queryDataMap 前台传输查询条件
     * @return String
     */
    @Override
    public String getWorkCoordinationList(Integer pageIndex,Integer pageSize,String departId,int level,String orgid,Map<String,String> queryDataMap) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        //本系统的本地code
        String localCode = CodeUrlUtils.getLocalCode();
        List<Map<String,String>> workCoordinationList = workCoordinationMapper.getWorkCoordinationList(page,departId,level,orgid,queryDataMap);
        for(Map<String,String> mapList : workCoordinationList) {
            for(Map.Entry<String,String> entry : mapList.entrySet()) {
                if("operateState".equals(entry.getKey())) {
                    String operateStateValue = entry.getValue();
                    //第三方司法厅code
                    String dssftDepartid = mapList.get("dssftDepartid");
                    //本地省份code与第三方相同 将operateState值修改为：1@1@1
                    if(localCode.equals(dssftDepartid)) {
                        mapList.put("operateState",operateStateValue + "@1");
                    } else {
                        mapList.put("operateState",operateStateValue + "@0");
                    }
                    break;
                }
            }
        }
        page = page.setRecords(workCoordinationList);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 上报按钮(获取基本信息)
     * @param priNum 服刑人员编号
     * @return String
     */
    @Override
    public String getInterProvincialBusinessInfo(String priNum) {
        Map<String,String> business = workCoordinationMapper.getInterProvincialBusinessByPriNum(priNum);
        return JsonUtil.toText(business);
    }

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 上报 - 保存按钮
     * @param data 页面真实数据
     * @param user user
     * @return Result
     */
    @Override
    public Result saveApply(String data,UserDTO user) {
        //更新xtjc_ksywxt表的司法厅电话联系人意见信息，state更新为0，本次不修改xjxx表的second_state
        Map<String,Object> paramMap = JsonUtil.toMap(data);
        int updateStatus = workCoordinationMapper.updateApplyData(paramMap);
        if(0 < updateStatus) {
            return Result.success(ReturnCode.SUCCESS);
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 第三省份 处理(同意或拒绝)
     * @param data 页面真实数据
     * @param user user
     * @return Result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveDeal(String data,UserDTO user) {
        //参数转换
        Map<String,Object> paramMap = JsonUtil.toMap(data);
        int updateStatus = 0;
        String flag = paramMap.get("flag").toString();
        //第三省份同意指定司法所 需要将xjxx表省市区县，second_state=5更新 和 xtjc_ksywxt 表state字段更新为2
        if(Constants.NumberStr.NUM_STR_ONE.equals(flag)) {
            updateStatus = workCoordinationMapper.updateDealAgree(paramMap);
        } else if(Constants.NumberStr.NUM_STR_TWO.equals(flag)) {
            //第三省份拒绝  需要将xtjc_ksywxt的第三司法厅的联系人电话意见更新 state=3  本次不修改xjxx表的second_state
            updateStatus = workCoordinationMapper.updateDealRefuse(paramMap);
            if(0 < updateStatus) {
                String priNum = paramMap.get("priNum").toString();
                switchToOriginalInvestigationOrganMapper.deletePrisonerMain(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisaveTemp(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerCjpg(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerFx(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerHc(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerJxjs(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerSr(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonersZyjn(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerXjxx(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerZn(priNum);
                switchToOriginalInvestigationOrganMapper.deletePrisonerZz(priNum);
                switchToOriginalInvestigationOrganMapper.deleteUploadAppendixFile(priNum);
            }
        }
        if(0 < updateStatus) {
            return Result.success(ReturnCode.SUCCESS);
        }
        return Result.failure(ReturnCode.FAILURE);
    }

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 回显抄送单位
     * @param departId 机构编码
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> getDepartmentCoordinate(String departId) {
        List<SystemCode> systemCodes = systemCodeMapper.getDepartmentCoordinate(departId);
        List<Map<String,Object>> list = new ArrayList<>(16);
        for(SystemCode sc : systemCodes) {
            Map<String,Object> map = new HashMap<>(16);
            map.put("codeId",sc.getCodeId());
            map.put("codeName",sc.getCodeName());
            list.add(map);
        }
        return list;
    }

    /**
     * 安置帮教管理 - 业务协调 - 业务协调 - 原司法厅上报协调失败后指定本省司法所
     * @param data 业务数据
     * @param user 当前登录用户
     * @return Result
     */
    @Override
    public Result saveDealOrigin(String data,UserDTO user) {
        Map<String,Object> paramMap = JsonUtil.toMap(data);
        //需要将xtjc_ksywxt的 state=4   需要将xjxx表省市区县   本次不修改xjxx表的second_state
        int updateStatus = workCoordinationMapper.updateDealOrigin(paramMap);
        if(0 < updateStatus) {
            return Result.success(ReturnCode.SUCCESS);
        }
        return Result.failure(ReturnCode.FAILURE);
    }
}